package com.app.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.app.common.TotalConfig;
import com.app.model.SysUser;
import com.app.pojo.PublicAnnouncement;
import com.app.service.IPublicAnnouncementService;
import com.app.service.ISysUserService;
import com.app.util.Result;
import com.app.vo.AdminPublicAnnouncementVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@RestController
@RequestMapping("/adminNotice")
@Api(tags = "admin通知公告接口")
public class AdminPublicAnnouncementController {
    @Autowired
    private IPublicAnnouncementService publicAnnouncementService;
    @Autowired
    private ISysUserService sysUserService;


    /**
     * 通知公告列表查询
     *
     * @param type  1公告 2通知
     * @param page
     * @param limit
     * @param title 搜索条件（模糊查询）
     * @return
     */
    @ApiOperation(value = "列表查询 ")
    @GetMapping("/list")
    public Result<AdminPublicAnnouncementVo> noticeList(Integer type, Integer page, Integer limit, String title) {
        return publicAnnouncementService.noticeList(type, page, limit, title);
    }


    /**
     * 通知公告的详情
     *
     * @param noticeId
     * @return
     */
    @ApiOperation(value = "通知公告的详情")
    @GetMapping("/noticeById")
    public Result<PublicAnnouncement> noticeDetial(Integer noticeId) {
        PublicAnnouncement byId = publicAnnouncementService.getById(noticeId);
        if (byId == null) {
            return Result.error("公告不存在");
        }
        LocalDateTime localDateTime = byId.getCreateTime();
        // 创建一个DateTimeFormatter实例，用于格式化日期时间
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:MM");
        String formattedDateTime = localDateTime.format(formatter);
        byId.setCreateTime(LocalDateTime.parse(formattedDateTime, formatter));
        return Result.success(byId);
    }

    @ApiOperation(value = "新增、修改")
    @PostMapping("/addAndUpdate")
    public Result<Boolean> addAndUpdate(@RequestBody PublicAnnouncement publicAnnouncement) {
        if (!StpUtil.hasPermission("admin:notice:save")) {
            return Result.error(TotalConfig.NO_PERMISSION);
        }
        SysUser sysUser = sysUserService.getById(StpUtil.getLoginIdAsLong());
        Integer userId = sysUser.getId();
        return publicAnnouncementService.addAndUpdate(publicAnnouncement, userId);
    }


    @ApiOperation(value = "删除")
    @GetMapping("/delete")
    public Result<Object> delete(Integer noticeId) {
        if (!StpUtil.hasPermission("admin:notice:delete")){
            return Result.error(TotalConfig.NO_PERMISSION);
        }
        PublicAnnouncement publicAnnouncement = new PublicAnnouncement();
        publicAnnouncement.setId(noticeId);
        publicAnnouncement.setStatus(0);
        publicAnnouncementService.updateById(publicAnnouncement);
        return Result.success("删除成功");
    }
}

